﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditCustomerType : BasePage
    {
        CustomerType custTypes;
        int custTypeID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            custTypes = (CustomerType)Session[Request.QueryString[KEY]];
            string oldFilter = custTypes.Filter;

            custTypeID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                custTypes.Filter = CustomerType.ColumnNames.ID + "=" + custTypeID.ToString();

            if (!IsPostBack && custTypes.RowCount > 0 && mode != ADD_MODE)
                LoadInfo();

            custTypes.Filter = oldFilter;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            string oldFilter = custTypes.Filter;
            try
            {
                if (mode == ADD_MODE)
                {
                    custTypes.AddNew();
                    custTypes.BranchID = Branches.CurrentBranchID;
                    custTypes.Deleted = false;
                }
                else
                {
                    custTypes.Filter = CustomerType.ColumnNames.ID + "=" + custTypeID;
                }

                custTypes.LastModifiedByUser = SessionUser.ID;
                custTypes.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                custTypes.TypeName = editCustomerType.TypeName;
                custTypes.DealerPrice = false;//no longer used
                custTypes.Price = editCustomerType.CustomerPrice;

                custTypes.Save();
                custTypes.Filter = oldFilter;
                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                custTypes.RejectChanges();
                custTypes.Filter = oldFilter;

                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editCustomer.aspx.cs", "Save", ex.StackTrace);
                return false;
            }
        }

        private void LoadInfo()
        {
            try
            {
                if (!custTypes.IsColumnNull(CustomerType.ColumnNames.TypeName))
                    editCustomerType.TypeName = custTypes.TypeName;
                if (!custTypes.IsColumnNull(CustomerType.ColumnNames.Price))
                    editCustomerType.CustomerPrice = custTypes.Price;
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editCustomer.aspx.cs", "LoadInfo", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}
